const api = require("../../../ws_micro_shop/utils/api.js");
const app = getApp();

let is_no_more = false;
let is_loading = false;
let p = 2;

Page({
  data: {
    status: -1,
    list: [],
    hidden: -1,
    theme: app.themeData,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: "暂无明细"
    },
    isShowLoading: false,
    is_show_line: false,
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let that = this;
    let commission_alias = decodeURIComponent(options.title);
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
        commission_alias: commission_alias
      });
    });
    is_no_more = false;
    is_loading = false;
    p = 2;
    this.GetList(options.status || -1);
  },
  switchlist(e) {
    let status = e.currentTarget.dataset.status;
    is_no_more = false;
    is_loading = false;
    p = 2;
    wx.pageScrollTo({
      scrollTop: 0
    });
    this.setData({
      is_show_line: false,
    })
    this.GetList(status);
  },
  GetList(status) {
    let that = this;
    that.setData({
      status: parseInt(status || -1, 0),
      isShowLoading: false,
    });
    wx.showLoading({
      title: "正在加载...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.share.get_order,
      data: {
        status: that.data.status
      },
      success: function(res) {
        that.setData({
          list: res.data || []
        });
      },
      complete: function() {
        wx.hideLoading();
        that.setData({
          isShowLoading: true,
        })
      }
    });
  },
  click(e) {
    let index = Number(e.currentTarget.dataset.index);
    this.setData({
      hidden: Number(this.data.hidden) === index ? -1 : index
    });
  },
  onReachBottom() {
    let that = this;
    if (is_loading || is_no_more) {
      return;
    }
    is_loading = true;
    wx.showLoading({
      title: "加载更多...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.share.get_order,
      data: {
        status: that.data.status,
        page: p
      },
      success: function(res) {
        if (res.code === 0) {
          
          wx.hideLoading();
          if (res.data.length === 0) {
            is_no_more = true;
            that.setData({
              is_show_line: p > 1 ? true : false,
            })
          } else {
            let list = that.data.list.concat(res.data);
            that.setData({
              list
            });
            p++;
          }
        }
        
      },
      complete: function() {
        is_loading = false;
      }
    });
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
  checkOrderDetail(e) {
    let { id } = e.currentTarget.dataset;
    app.util.navigateTo(
      "ws_micro_shop/pages/order-detail/order-detail",
      { id, admin: "yes" },
      false
    );
  }
});